import { createI18n, type LocaleMessageDictionary, type VueMessageType } from 'vue-i18n';
import { ref } from 'vue';

import { localeEN, localeAR, localeZH, localeUR, localeTR, localeIN } from './messages';

const i18n = createI18n({
    legacy: false, // you must set `false`, to use Composition API
    allowComposition: true, // you need to specify that!

    globalInjection: true,
    locale: window.sessionStorage.getItem('language') || 'en', // 语言标识, 通过切换locale的值来实现语言切换
    fallbackLocale: {
        'zh-cn': ['zh'],
        'default': ['en'],
    },
    formatFallbackMessages: true, // 对翻译键执行模板插值
});
i18n.global['isRtl'] = ref(
    ['ar', 'ur'].includes(<string>window.sessionStorage.getItem('language'))
);
i18n.global['direction'] = ref(
    ['ar', 'ur'].includes(<string>window.sessionStorage.getItem('language'))
        ? 'direction: rtl;'
        : ''
);

i18n.global.setLocaleMessage('en', <LocaleMessageDictionary<VueMessageType>>localeEN);
i18n.global.setLocaleMessage('ar', <LocaleMessageDictionary<VueMessageType>>localeAR);
i18n.global.setLocaleMessage('in', <LocaleMessageDictionary<VueMessageType>>localeIN);
i18n.global.setLocaleMessage('tr', <LocaleMessageDictionary<VueMessageType>>localeTR);
i18n.global.setLocaleMessage('ur', <LocaleMessageDictionary<VueMessageType>>localeUR);
i18n.global.setLocaleMessage('zh', <LocaleMessageDictionary<VueMessageType>>localeZH);

export default i18n;
